<?php

/**
 * Override of theme_breadcrumb().
 */
function informationweek_breadcrumb($variables) {
    $breadcrumb = $variables['breadcrumb'];
    if (!empty($breadcrumb)) {
        // Provide a navigational heading to give context for breadcrumb links to
        // screen-reader users. Make the heading invisible with .element-invisible.
        $output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';

        $output .= '<div class="breadcrumb">' . implode(' › ', $breadcrumb) . '</div>';
        return $output;
    }
}

/**
 * Override or insert variables into the maintenance page template.
 */
function informationweek_preprocess_maintenance_page(&$vars) {
    // While markup for normal pages is split into page.tpl.php and html.tpl.php,
    // the markup for the maintenance page is all in the single
    // maintenance-page.tpl.php template. So, to have what's done in
    // informationweek_preprocess_html() also happen on the maintenance page, it has to be
    // called here.
    informationweek_preprocess_html($vars);
}

/**
 * Override or insert variables into the html template.
 */
function informationweek_preprocess_html(&$vars) {
    // Toggle fixed or fluid width.
    if (theme_get_setting('informationweek_width') == 'fluid') {
        $vars['classes_array'][] = 'fluid-width';
    }
    // Add conditional CSS for IE6.
    drupal_add_css(path_to_theme() . '/fix-ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lt IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
}

/**
 * Override or insert variables into the html template.
 */
function informationweek_process_html(&$vars) {
    // Hook into color.module
    if (module_exists('color')) {
        _color_html_alter($vars);
    }
}


/**
 * Override or insert variables into the page template.
 */
function informationweek_preprocess_page(&$vars) {
    // Move secondary tabs into a separate variable.
    $vars['tabs2'] = array(
        '#theme' => 'menu_local_tasks',
        '#secondary' => $vars['tabs']['#secondary'],
    );
    unset($vars['tabs']['#secondary']);
    if (isset($vars['main_menu'])) {
        $vars['primary_nav'] = theme('links__system_main_menu');
    } else {
        $vars['primary_nav'] = FALSE;
    }
    if (isset($vars['secondary_menu'])) {
        $vars['secondary_nav'] = theme('links__system_secondary_menu', array(
            'links' => $vars['secondary_menu'],
            'attributes' => array(
                'class' => array('links', 'inline', 'secondary-menu'),
            ),
            'heading' => array(
                'text' => t('Secondary menu'),
                'level' => 'h2',
                'class' => array('element-invisible'),
            )
        ));
    } else {
        $vars['secondary_nav'] = FALSE;
    }

    // Prepare header.
    $site_fields = array();
    if (!empty($vars['site_name'])) {
        $site_fields[] = $vars['site_name'];
    }
    if (!empty($vars['site_slogan'])) {
        $site_fields[] = $vars['site_slogan'];
    }
    $vars['site_title'] = implode(' ', $site_fields);
    if (!empty($site_fields)) {
        $site_fields[0] = '<span>' . $site_fields[0] . '</span>';
    }
    $vars['site_html'] = implode(' ', $site_fields);

    // Set a variable for the site name title and logo alt attributes text.
    $slogan_text = $vars['site_slogan'];
    $site_name_text = $vars['site_name'];
    $vars['site_name_and_slogan'] = $site_name_text . ' ' . $slogan_text;


    /* Set a variable for navigator menu on top of page */
    $vars['meta_menu'] = theme('links__system_meta_menu');
    $vars['main_menu'] = theme('links__system_main_menu');
}

function informationweek_links__system_meta_menu() {
    $meta_menu = menu_tree_all_data('menu-meta-menu');
    $html = '<div class="meta-menu">';
    $html .='<nav><ul class="sections">';
    foreach ($meta_menu as $item) {
        if ($item['link']['in_active_trail'])
            $curr = 'current';
        if (!empty($item['below'])) {
            $html .= "<li class ='more'><a class='{$curr}' href='{$item['link']['href']}' title ='{$item['link']['title']}'><span>{$item['link']['title']}</span></a>";
            $html .= '<ul class="sub-menu">';
            foreach ($item['below'] as $submenu) {
                $html.="<li><a href='{$submenu['link']['href']}' title ='{$submenu['link']['title']}'>{$submenu['link']['title']}</a></li>";
            }
            $html .='</ul>';
            $html .='</li>';
        }
        else
            $html .= "<li><a class='{$curr}' href='{$item['link']['href']}' title ='{$item['link']['title']}'><span>{$item['link']['title']}</span></a></li>";
    }
    $html .= '</ul></nav>';
    $html .='</div>';
    return $html;
}

function informationweek_links__system_main_menu() {
    $main_menu = menu_tree_all_data('main-menu');
    $html = '<div class="main-menu">';
    $html .= '<nav><ul>';
    foreach ($main_menu as $item) {
        $href = url($item['link']['href']);
        $actives = menu_get_active_trail();
        if ($item['link']['mlid'] == $actives[1]['mlid']){
            $html .="<li><a class='current' href='{$href}' title ='{$item['link']['title']}'><span>{$item['link']['title']}</span></a>";
            if(!empty($item['below'])){
                $html.='<ul class="sub-main-menu">';
                foreach($item['below'] as $submenu){
                    $href = url($submenu['link']['href']);
                    $html .= "<li><a href='{$href}' title='{$submenu['link']['title']}'>{$submenu['link']['title']}</a></li>";
                }
                $html .='<div style="clear:both;"></div>';
                $html.='</ul>';
            }
            $html .= "</li>";
        }
        else
            $html .="<li><a href='{$href}' title ='{$item['link']['title']}'>{$item['link']['title']}</a></li>";
    }
    $html .= '</nav></ul>';
    $html .='</div>';
    return $html;
}

/**
 * Override or insert variables into the node template.
 */
function informationweek_preprocess_node(&$vars) {
    $vars['submitted'] = $vars['date'] . ' — ' . $vars['name'];
}

/**
 * Override or insert variables into the comment template.
 */
function informationweek_preprocess_comment(&$vars) {
    $vars['submitted'] = $vars['created'] . ' — ' . $vars['author'];
}

/**
 * Override or insert variables into the block template.
 */
function informationweek_preprocess_block(&$vars) {
    $vars['title_attributes_array']['class'][] = 'title';
    $vars['classes_array'][] = 'clearfix';
}

/**
 * Override or insert variables into the page template.
 */
function informationweek_process_page(&$vars) {
    // Hook into color.module
    if (module_exists('color')) {
        _color_page_alter($vars);
    }
}

/**
 * Override or insert variables into the region template.
 */
function informationweek_preprocess_region(&$vars) {
    if ($vars['region'] == 'header') {
        $vars['classes_array'][] = 'clearfix';
    }
}
